//
//  JLCityTableViewController.m
//  JLStyleApp
//
//  Created by LvJianfeng on 15/8/21.
//  Copyright (c) 2015年 JLStyle. All rights reserved.
//

#import "JLCityTableViewController.h"
#import "JLSelectCityTableViewController.h"
#import "JLCreatePersonMegViewController.h"
#import "JLCommon.h"

@interface JLCityTableViewController () <ChooseDelegate>
{
    City *temp;
}
@property (strong, nonatomic) NSArray *data;
@end

@implementation JLCityTableViewController

- (void)initData{
    JLCommon *shareManager = [JLCommon shareManager];
    self.data = [shareManager get_Provinces];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    EmtryCellHidden(self.tableView);
    
    [self initData];
    
    [self setupRightBarItem];
    self.title = @"选择城市";
}

- (void)cancelAction{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}


- (void)viewDidAppear:(BOOL)animated{
    if (temp) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:NO completion:^{
                [self.delegate cityPass:temp];
            }];
        });
    }
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *Identifier = @"CityCell";
    JLCityCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell==nil) {
        cell = [[JLCityCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    City *city = self.data[indexPath.row];
    cell.labelCityNameLabel.text = city.name;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    City *city = self.data[indexPath.row];
    JLSelectCityTableViewController *selectTableViewController = [[JLSelectCityTableViewController alloc] init];
    selectTableViewController.pid = city.cid;
    selectTableViewController.delegate = self;
    [self.navigationController pushViewController:selectTableViewController animated:YES];
}

- (void)setupRightBarItem{
    UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
    doneButton.titleLabel.font = JLFont(15);
    [doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor customLightPlaceColor] forState:UIControlStateHighlighted];
    [doneButton setTitle:@"取消" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}

#pragma mark ChooseDele gate
- (void)chooseCityAction:(City *)city{
    temp = city;
}

#pragma mark lazyload
- (NSArray *)data{
    if (_data==nil) {
        _data = [[NSArray alloc] init];
    }
    return _data;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

@implementation JLCityCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //self.selectionStyle     = UITableViewCellSelectionStyleNone; //点击无背景
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.backgroundColor    = [UIColor clearColor];
        UILabel *cityNameLabel  = [[UILabel alloc] init];
        cityNameLabel.frame         = CGRectMake(20, 50/2-20, ScreenWidth-35, 40);
        [self addSubview:cityNameLabel];
        self.labelCityNameLabel    = cityNameLabel;
    }
    return self;
}
@end
